<template>
	<div class="tCourseList w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<button @click="reBack"><i><&nbsp;</i>返回</button>
			<p>{{updateTitle}}</p>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<a class="listOne" @click="createJsCourse">新增课程</a>
				<a class="listTwo">导出数据</a>
				<el-cascader v-model="searchSelect" :options="options" :props="optionProps" :show-all-levels='false' filterable placeholder="课程分类" @change="getTableList" class="th_2"></el-cascader>
				</el-cascader>
				<div class="msearch">
					<input placeholder="请输入课程名称关键词..." v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column prop="courseName" label="课程名称"></el-table-column>
				<el-table-column label="课程数据" width="140">
					<template slot-scope="scope">
						<el-button @click="liveData(scope.row)">查看详情</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200">
					<template slot-scope="scope">
						<el-button @click="handleEdit(scope.row)">编辑</el-button>
						<el-button @click="handlecomments(scope.row)" class="blue">评论</el-button>
						<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll" class="allDelete">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				nickname: '',
				search:'',
				searchSelect: [],
				courseColumnYi:'',
				tableData: [],
				pagesize: 15,
				currentpage: 1,
				total: 1,
				deleteType:'delete',
				multipleSelection: [],
				options: [],
				optionProps: {
                    value: 'id',
                    label: 'columnName',
                    children: 'childList'
                },
				updateTitle:'',
				speakerId:'', //从讲师页面带过来的id
				nowBack:''
			}
		},
		methods: {
			getParams() {
				this.updateTitle = this.$route.params.updateTitle;
				this.speakerId = this.$route.params.speakerId;
				this.speakerName = this.$route.params.speakerName;
				this.nowBack = this.$route.params.nowBack;
				console.log('tCourseList:',this.$route.params);
			},
			getTableList(){
				var s={
				    page: this.currentpage,
					num: this.pagesize,
					fuzzyFields: this.search,
					speakerId:this.speakerId,
					rootcompanyid:1,
					courseType:1
                };
				if(this.searchSelect!=null && this.searchSelect.length >= 2){
                	s.courseColumnYi = this.searchSelect[0];
					s.courseColumn = this.searchSelect[1];
                };
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/kcCourse/selectKcCourseList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: s,
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
						}
					}
				});
			},
			createJsCourse(){
				this.$router.push({
					name: 'teacherCourse',
					params: {
						updateTitle:this.updateTitle,
						speakerId:this.speakerId,
						speakerName:this.speakerName
					}
				})
			},
			handleEdit(row) {
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/kcCourse/getCourseDetails",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {id:row.id},
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'teacherCourse',
								params: {
									type: 'edit',
									updateTitle:this.updateTitle,
									speakerId:this.speakerId
								}
							})
							this.$root.managerMsg = res.data.data;
						}
					}
				});
			},
			handlecomments(row) {
				this.$router.push({
					name: 'addcomment',
					params: {
						courseId: row.id,
						courseName:row.courseName
					}
				})
			},
			liveData(row){
				this.$router.push({
					name: 'liveData',
					params: {
						liveId:row.id,
						updateTitle:row.courseName,
						teacherTitle:this.updateTitle,
						lecturerid:this.speakerId,
						pathKey:'tCourseList',
						nowBack:this.nowBack
					}
				})
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				for(var i = 0;i<val.length;i++){
					var s={id:val[i].id}
					this.multipleSelection.push(s)
				}
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var list = [];
					list.push({
						id:this.transitionRow.id
					});
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourse/deleteKcCourseState",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourse/deleteKcCourseState",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:this.multipleSelection,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			getTreeData(data){
				for(var i=0;i<data.length;i++){
					if(data[i].childList.length<1){
						data[i].childList=undefined;
					}else {
						this.getTreeData(data[i].childList);
					}
				}
				return data;
        	},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			reBack(){
				console.log('nowBack:',this.nowBack);
				if(this.nowBack==1){
					this.$router.push({
						name:'teacher'
					});
				}else if(this.nowBack==2){
					this.$router.push({
						name:'rankJs'
					});
				}
			},
			getColumnList(){
				let columnType = [1];
				this.$rootFunction().columnList({columnType});
			},
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getParams();
			this.getColumnList();
			if(this.speakerId==null){
				this.$store.commit('GET_TEA_KCLIST');
				var s =this.$store.state.teaKcList;
				this.updateTitle=s.updateTitle;
				this.speakerId=s.speakerId;
				this.speakerName = s.speakerName;
				this.nowBack = s.nowBack
			}
			this.getTableList();
			window.addEventListener("beforeunload",()=>{
				s={
					updateTitle : this.updateTitle,
					speakerId : this.speakerId,
					speakerName:this.speakerName,
					nowBack:this.nowBack
				}
				this.$store.commit('SET_TEA_KCLIST', s);
			});
		}
	}
</script>